<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-circle-plus-outline"
          size="medium"
          @click="handleAdd"
          v-hasPermi="['template:management:add']"
          >添加规则</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="medium"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['template:management:remove']"
          >删除规则</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-circle-close"
          size="medium"
          :loading="exportLoading"
          @click="handleClose"
          v-hasPermi="['template:management:export']"
          >关闭</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList()"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="fieldRuleTableData"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="字段ID" align="center" prop="fieldId" />
      <el-table-column label="数据处理规则" align="center" prop="processType">
        <template slot-scope="scope">
          <span v-if="scope.row.processType === 'replace'">替换</span>
          <span v-if="scope.row.processType === 'substrbefore'">截取之前</span>
          <span v-if="scope.row.processType === 'substrafter'">截取之后</span>
        </template>
      </el-table-column>
      <el-table-column label="替换正则" align="center" prop="replacereg" />
      <el-table-column
        label="替换内容"
        align="center"
        width="355"
        prop="replacement"
      />
      <el-table-column
        label="截取字符串目标"
        align="center"
        prop="substrTarget"
      />
      <el-table-column label="排序" align="center" prop="sort" />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="medium"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['template:management:edit']"
            >编辑</el-button
          >
          <el-button
            size="medium"
            type="text"
            icon="el-icon-delete-solid"
            @click="handleDelete(scope.row)"
            v-hasPermi="['template:management:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="searchParams.pageNum"
      :limit.sync="searchParams.pageSize"
      @pagination="getList()"
    />

    <!-- 添加或修改字段规则对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="数据处理规则" prop="processType">
          <el-radio-group v-model="form.processType">
            <el-radio label="replace">替换</el-radio>
            <el-radio label="substrbefore">截取之前</el-radio>
            <el-radio label="substrafter">截取之后</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="替换正则"
          v-if="form.processType === 'replace'"
          prop="replacereg"
        >
          <el-input v-model="form.replacereg" placeholder="请输入替换正则" />
        </el-form-item>
        <el-form-item
          label="替换内容"
          v-if="form.processType === 'replace'"
          prop="replacement"
        >
          <el-input v-model="form.replacement" placeholder="请输入替换内容" />
        </el-form-item>
        <el-form-item label="截取字符串目标" prop="substrTarget">
          <el-input
            v-model="form.substrTarget"
            placeholder="请输入截取字符串目标"
          />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryFieldRule,
  delFieldRule,
  addFieldRule,
  updateFieldRule
} from '@/api/crawler/crawler'

export default {
  name: 'FieldRule',
  components: {},
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字段规则表格数据
      fieldRuleTableData: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      fileList: [],
      // 查询参数
      searchParams: {
        pageNum: 1,
        pageSize: 10,
        fieldId: ''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    const dictId = this.$route.params && this.$route.params.rowId
    this.searchParams.fieldId = Number(dictId)
    this.getList()
  },
  methods: {
    //  查询字段规则列表
    getList() {
      this.loading = true
      queryFieldRule(this.searchParams).then(response => {
        this.fieldRuleTableData = response.list
        this.total = response.total
        this.loading = false
      })
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        processType: undefined,
        replacereg: undefined,
        replacement: undefined,
        substrTarget: undefined,
        sort: undefined
      }
      this.resetForm('form')
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.searchParams.pageNum = 1
      this.getList()
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('searchForm')
      this.handleQuery()
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 关闭按钮操作 */
    handleClose() {
      this.$router.go(-1)
    },

    /** 添加按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.form = {
        processType: 'replace'
      }
      this.title = '添加字段规则模板'
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.form = {
        id: row.id,
        processType: row.processType,
        replacereg: row.replacereg,
        replacement: row.replacement,
        substrTarget: row.substrTarget,
        sort: row.sort
      }
      this.open = true
      this.title = '修改字段规则模板'
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.buttonLoading = true
          if (this.form.id != null) {
            updateFieldRule(this.form)
              .then(response => {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
              .finally(() => {
                this.buttonLoading = false
              })
          } else {
            this.form.fieldId = this.searchParams.fieldId
            addFieldRule(this.form)
              .then(response => {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
              .finally(() => {
                this.buttonLoading = false
              })
          }
        }
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids.join(',')
      this.$confirm(
        '是否确认删除字段规则编号为"' + ids + '"的数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          this.loading = true
          return delFieldRule(ids)
        })
        .then(() => {
          this.loading = false
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(() => {})
    }
  }
}
</script>
<style scoped lang="scss">
::v-deep .el-dialog__footer {
  text-align: center;
}
</style>